package com.handu.luna.boss.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.handu.luna.core.domain.Mall;
import com.handu.luna.core.domain.article.ArticleCategory;
import com.handu.luna.core.domain.goods.Category;

@Controller
@RequestMapping("/tree/*")
public class TreeJSONController extends BaseController {
	
	class Tree{
		public String id = "0";
		public String text = "";
		public String type = "";
		public State state = new State();
		public List<Tree> children;
	}
	class State{
		public String opened = "true";
		//public String disabled = "false";
	}
	
	@RequestMapping()
	public @ResponseBody Tree category() {   //商品分类
		Mall mall=new Mall();	
		mall.init();
		
		Tree tree = new Tree();
		tree.text = "顶层分类";
		tree.children = new ArrayList<Tree>();
		tree.type = "root";
		for(Category cat : mall.getCategories()){
			tree.children.add(categoryToTree(cat));
		}
		return tree;
 
	}
	
	private Tree categoryToTree(Category cat){
		Tree tree = new Tree();
		tree.id = cat.getId() + "";
		tree.text = cat.getName();
		tree.children = new ArrayList<Tree>();
		for(Category each : cat.getSubCategories()){
			tree.children.add(categoryToTree(each));
		}
		return tree;		
	}
	
	//文章分类
	@RequestMapping("/tree/categoryArticle")
	public @ResponseBody Tree categoryArticle() {
		Mall mall=new Mall();	
		mall.articleInit();
		
		Tree tree = new Tree();
		tree.text = "顶层分类";
		tree.children = new ArrayList<Tree>();
		tree.type = "root";
		for(ArticleCategory cat : mall.getArticleCategories()){  //mall.getArticleCategories()
			tree.children.add(categoryArticleToTree(cat));
		}
		return tree;
 
	}
	
	private Tree categoryArticleToTree(ArticleCategory cat){
		Tree tree = new Tree();
		tree.id = cat.getId() + "";
		tree.text = cat.getName();   
		tree.children = new ArrayList<Tree>();
		for(ArticleCategory each : cat.getSubCategories()){
			tree.children.add(categoryArticleToTree(each));
		}
		return tree;		
	}
}
